package com.woniuxy.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.reader.entity.Book;
import com.woniuxy.reader.entity.Collection;
import com.woniuxy.reader.mapper.BookMapper;
import com.woniuxy.reader.mapper.CollectionMapper;
import com.woniuxy.reader.service.CollectionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zh_o
 * @since 2020-10-12
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {
    @Resource
    private BookMapper bookMapper;
    @Resource
    private CollectionMapper collectionMapper;
    @Override
    public List<Book> getBooks(Integer userId) throws Exception {
        //收藏表
        QueryWrapper<Collection> queryWrapper  = new QueryWrapper<Collection>();
        //从收藏表中通过userId查找到Collection集合
        queryWrapper.eq("user_id", userId);
        List<Collection> lists=collectionMapper.selectList(queryWrapper);
        //书籍表
        QueryWrapper<Book> queryWrapper1  = new QueryWrapper<Book>();
        //取出收藏集合中的所有书籍id
        List<Integer> list=new ArrayList<>();
        for(Collection con:lists) {
            Integer bookId=con.getBookId();
            list.add(bookId);
        }
        //从书籍表中查找集合中bookId的所有集合信息
        queryWrapper1.in("book_id",list);
        List<Book> list2=bookMapper.selectList(queryWrapper1);
        return list2;
    }

    @Override
    public int deleteBook(Integer bid, Integer uid) throws Exception {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("user_id", uid);
        columnMap.put("book_id", bid);
        int rows=collectionMapper.deleteByMap(columnMap);
        return rows;
    }
}
